package com.xctech.yace.util;

import com.xctech.yace.pojo.DbCompareRes;
import com.xctech.yace.pojo.FileCompareRes;
import org.apache.poi.hssf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class ExcelUtil {


    public static void writeDbRes(String[] title, String path, List<DbCompareRes> data) {
        //创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建sheet
        HSSFSheet sheet = workbook.createSheet("result");
        sheet.setColumnWidth(0, 3000);
        sheet.setColumnWidth(1, 3000);
        sheet.setColumnWidth(2, 3000);
        sheet.setColumnWidth(3, 3000);
        //创建第一行
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = null;
        //插入第一行数据表头
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
        }
        for (int i = 1; i <= data.size(); i++) {
            HSSFRow row2 = sheet.createRow(i);
            HSSFCell cell0 = row2.createCell(0);
            cell0.setCellValue(data.get(i - 1).getTableName());
            cell0 = row2.createCell(1);
            cell0.setCellValue(data.get(i - 1).getColumnName());
            cell0 = row2.createCell(2);
            cell0.setCellValue(data.get(i - 1).getOriType());
            cell0 = row2.createCell(3);
            cell0.setCellValue(data.get(i - 1).getNewType());
        }
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(path);
            workbook.write(fileOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void writeFileRes(String[] title, String path, List<FileCompareRes> data) {
        //创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建sheet
        HSSFSheet sheet = workbook.createSheet("result");
        sheet.setColumnWidth(0, 3000);
        sheet.setColumnWidth(1, 6000);
        sheet.setColumnWidth(2, 30000);
        //创建第一行
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = null;
        //插入第一行数据表头
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
        }
        for (int i = 1; i <= data.size(); i++) {
            HSSFRow row2 = sheet.createRow(i);
            HSSFCell cell0 = row2.createCell(0);
            cell0.setCellValue(data.get(i - 1).getTableName());
            cell0 = row2.createCell(1);
            cell0.setCellValue(data.get(i - 1).getVersion());
            cell0 = row2.createCell(2);
            cell0.setCellValue(data.get(i - 1).getContent());
        }
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(path);
            workbook.write(fileOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
